package ok;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    private JPanel contentPane;
    private JTextField idtext;
    public static registerFrame registerframe;
    public static User user[]=new User[10];
    public static HistoryQuizSystem historyQuizSystem;
    public static int userLength;
    private JPasswordField passwordtext;
    /**
     * Create the frame.
     */
    public static void main(String args[]) {
        LoginFrame loginframe=new LoginFrame();
        loginframe.setVisible(true);
    }

    public LoginFrame() {
        userLength=3;
        user[0]=new User("111","111");
        user[1]=new User("222","222");
        user[2]=new User("333","333");
        registerframe=new registerFrame();
        registerframe.setVisible(false);
        historyQuizSystem=new HistoryQuizSystem();
        historyQuizSystem.setVisible(false);

        setTitle("刷题系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 464, 361);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("刷题系统登录");
        lblNewLabel.setFont(new Font("华文仿宋", Font.BOLD, 20));
        lblNewLabel.setBounds(152, 24, 140, 36);
        contentPane.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("\u8D26\u53F7\uFF1A");
        lblNewLabel_1.setFont(new Font("华文仿宋", Font.BOLD, 20));
        lblNewLabel_1.setBounds(50, 98, 73, 19);
        contentPane.add(lblNewLabel_1);

        JLabel lblNewLabel_1_1 = new JLabel("\u5BC6\u7801\uFF1A");
        lblNewLabel_1_1.setFont(new Font("华文仿宋", Font.BOLD, 20));
        lblNewLabel_1_1.setBounds(50, 149, 73, 21);
        contentPane.add(lblNewLabel_1_1);

        idtext = new JTextField();
        idtext.setBounds(152, 96, 189, 21);
        contentPane.add(idtext);
        idtext.setColumns(10);

        JButton btnNewButton = new JButton("\u767B\u5F55");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                loginButton(e);
            }
        });
        btnNewButton.setBounds(302, 259, 97, 29);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("\u6CE8\u518C");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                landButton(e);
            }
        });
        btnNewButton_1.setBounds(50, 259, 97, 29);
        contentPane.add(btnNewButton_1);

        passwordtext = new JPasswordField();
        passwordtext.setBounds(152, 149, 189, 21);
        contentPane.add(passwordtext);
    }
    protected void landButton(ActionEvent e) {
        // TODO Auto-generated method stub
        registerframe.setVisible(true);
        this.setVisible(false);
    }
    protected void loginButton(ActionEvent e) {
        // TODO Auto-generated method stub
        String id=idtext.getText();
        String password=passwordtext.getText();
        int i;
        for(i=0;i<userLength;i++) {
            if(id.equals(user[i].id)&&password.equals(user[i].password)) {
                historyQuizSystem.setVisible(true);
                this.setVisible(false);
                break;
            }
        }
        if(i==userLength) {
            JOptionPane.showMessageDialog(null, "账号或密码错误！", "提示",JOptionPane.ERROR_MESSAGE);
        }
    }
}

